package com.heima.comment.service.impl;

import com.heima.apis.user.IUSerCilent;
import com.heima.comment.pojos.ApAssociateWords;
import com.heima.comment.service.CommentRepayService;
import com.heima.common.aliyun.GreenTextScan;
import com.heima.model.comment.dtos.CommentRepayDto;
import com.heima.model.comment.dtos.CommentRepayLikeDto;
import com.heima.model.comment.dtos.CommentRepaySaveDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.thread.AppThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author: xrl
 * @Date: 2022/10/23  19:35
 * @Version 1.0
 */
@Service
@Transactional
@Slf4j
public class CommentRepayServiceImpl implements CommentRepayService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private GreenTextScan textScan;

    @Autowired
    private IUSerCilent iuSerCilent;

    /**
     * 回复评论保存
     */
    @Override
    public ResponseResult saveCommentRepay(CommentRepaySaveDto dto) {
        //1.检查参数
        if (dto == null || dto.getCommentId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2.是否登录
        ApUser user = AppThreadLocalUtil.getUser();
        if (user == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }

        if (dto.getContent().length() > 140) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "评论内容不能超过140字");
        }

        try {
            //文本垃圾检测
            Map map = textScan.greeTextScan(dto.getContent());
            if (map != null) {
                //审核失败
                if (map.get("suggestion").equals("block")) {
                    return ResponseResult.errorResult(AppHttpCodeEnum.EXCEL_REPORT_FAIL, "评论具有违规内容");
                }
            }
        } catch (Exception e) {
            return ResponseResult.errorResult(AppHttpCodeEnum.EXCEL_REPORT_FAIL, "评论具有违规内容");
        }

        //获取评论的用户信息
        Integer id =  AppThreadLocalUtil.getUser().getId();
        ApUser user1 = iuSerCilent.getUser(id);

        //准备保存到MongoDB
        ApAssociateWords words = new ApAssociateWords();
        words.setCommentId(dto.getCommentId());
        words.setType((short) 1);
        words.setContent(dto.getContent());
        words.setCreatedTime(new Date());
        words.setAuthorName(user1.getName());
        words.setOperation((short) 0);
        mongoTemplate.save(words);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 点赞
     */
    @Override
    public ResponseResult likeCommentRepay(CommentRepayLikeDto dto) {
        //1.检查参数
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        ApAssociateWords template = mongoTemplate.findById(dto.getCommentRepayId(), ApAssociateWords.class);
        if ((dto.getOperation() == 0)) {
            //代表点赞
            //查询该评论
            template.setOperation((short) (template.getOperation() + 1));
            mongoTemplate.save(template);
        } else if (dto.getOperation() == 1) {
            //取消点赞
            template.setOperation((short) (template.getOperation() - 1));
            mongoTemplate.save(template);
        }

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 查询评论回复列表
     */
    @Override
    public ResponseResult loadCommentRepay(CommentRepayDto dto) {
        //1.检查参数
        if (dto == null || dto.getCommentId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2.是否登录
        ApUser user = AppThreadLocalUtil.getUser();
        if (user == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }

        Query query1 = new Query();
        Criteria criteria = new Criteria();
        criteria = Criteria.where("commentId").is(dto.getCommentId())
                .and("createdTime").lt(dto.getMinDate());
        query1.addCriteria(criteria);

        long totalCount = mongoTemplate.count(query1, ApAssociateWords.class);

        query1.with(Sort.by(Sort.Direction.DESC, "createdTime"));
        query1.limit(10);
        List<ApAssociateWords> data = mongoTemplate.find(query1, ApAssociateWords.class);

        return ResponseResult.okResult(data);
    }


}

